package org.yuanqiframework.yuanqi.boot.sys.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.yuanqiframework.yuanqi.web.controller.BaseController;
import org.yuanqiframework.yuanqi.core.utils.StringUtils;
import org.yuanqiframework.yuanqi.web.entity.AjaxResult;
import org.yuanqiframework.yuanqi.boot.sys.entity.SysLoginLog;
import org.yuanqiframework.yuanqi.boot.sys.entity.SysOperLog;
import org.yuanqiframework.yuanqi.boot.sys.service.SysLoginLogService;
import org.yuanqiframework.yuanqi.boot.sys.service.SysOperLogService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RequestMapping("/api/sys/log")
@RestController
public class LogController extends BaseController {
    @Resource
    private SysLoginLogService sysLoginLogService;
    @Resource
    private SysOperLogService sysOperLogService;


    @GetMapping(value = "login")
    public AjaxResult<IPage<SysLoginLog>> queryLoginLogPage(@RequestParam(required = false) String username,
                                                            @RequestParam(required = false) String ip,
                                                            @RequestParam(required = false) String status,
                                                            @RequestParam(required = true) Integer pageNum,
                                                            @RequestParam(required = true) Integer pageSize) {
        return AjaxResult.success(sysLoginLogService.page(new Page<SysLoginLog>(pageNum, pageSize),
                new LambdaQueryWrapper<SysLoginLog>()
                        .eq(StringUtils.isNotEmpty(username), SysLoginLog::getUsername, username)
                        .eq(StringUtils.isNotEmpty(ip), SysLoginLog::getIp, ip)
                        .eq(StringUtils.isNotEmpty(status), SysLoginLog::getStatus, status)
                        .orderByDesc(SysLoginLog::getLoginTime)
        ));
    }


    @GetMapping(value = "oper")
    public AjaxResult<IPage<SysOperLog>> queryOperLogPage(@RequestParam(required = false) String operName,
                                                          @RequestParam(required = false) String operType,
                                                          @RequestParam(required = false) String status,
                                                          @RequestParam(required = true) Integer pageNum,
                                                          @RequestParam(required = true) Integer pageSize) {
        return AjaxResult.success(sysOperLogService.page(new Page<SysOperLog>(pageNum, pageSize),
                new LambdaQueryWrapper<SysOperLog>()
                        .eq(StringUtils.isNotEmpty(operName), SysOperLog::getOperName, operName)
                        .eq(StringUtils.isNotEmpty(operType), SysOperLog::getOperType, operType)
                        .eq(StringUtils.isNotEmpty(status), SysOperLog::getStatus, status)
                        .orderByDesc(SysOperLog::getOperTime)
        ));
    }


}
